<?php 
namespace app\system\admin;
use app\system\admin\Admin;
use think\Db;
use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
use PhpOffice\PhpSpreadsheet\Reader\Xls;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Worksheet\PageSetup;
use PhpOffice\PhpSpreadsheet\Cell\DataType;
use PhpOffice\PhpSpreadsheet\Style\Fill;
use PhpOffice\PhpSpreadsheet\Style\Color;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
use PhpOffice\PhpSpreadsheet\Style\Border;
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;

/**
 *  @author Lin
 *  @time 2021/7/22
 */
class Quan extends Admin
{
    protected function initialize()
    {
        parent::initialize();
        set_time_limit(0);
       
    }

    function index(){
        // 产品分类
        $gwhere = [];
        if ($this->request->isAjax()) {

            $where[]      = ['is_delete','eq',0];
            $page       = $this->request->param('page/d', 1);
            $limit      = $this->request->param('limit/d', 15);
            $list = Db::name('product_quan')->where($where)->page($page)->limit($limit)->select();
            
            if($list){
                foreach($list as $key=>&$value){
                    $value['get_num'] = Db::name('user_quan')->where('quan_id',$value['id'])->count('id');
                    $value['use_num'] = Db::name('user_quan')->where(array('quan_id'=>$value['id'],'status'=>1))->count('id');

                    if($value['get_bg_time']){
                        $value['get_bg_time'] = date('Y-m-d H:i:s',$value['get_bg_time']);
                    }
                    if($value['get_end_time']){
                        $value['get_end_time'] = date('Y-m-d H:i:s',$value['get_end_time']);
                    }
                    if($value['use_bg_time']){
                        $value['use_bg_time'] = date('Y-m-d H:i:s',$value['use_bg_time']);
                    }
                    if($value['use_end_time']){
                        $value['use_end_time'] = date('Y-m-d H:i:s',$value['use_end_time']);
                    }

                    if($value['status']==1){
                        $value['status'] = '是';
                    }
                    else{
                        $value['status'] = '否';
                    }
                }
            }
            $data['data'] = $list;
            $data['count'] = Db::name('product_quan')->where($where)->count('id');
            $data['code'] = 0;
            return json($data);

        }
        return $this->fetch();
    }

    public function add(){
        if($this->request->isPost()){
            $list = $this->request->post();
            
            $list['ctime'] = time();
            $list['get_bg_time'] = $list['get_bg_time']?strtotime($list['get_bg_time']):0;
            $list['get_end_time'] = $list['get_end_time']?strtotime($list['get_end_time']):0;
            $list['use_bg_time'] = $list['use_bg_time']?strtotime($list['use_bg_time']):0;
            $list['use_end_time'] = $list['use_end_time']?strtotime($list['use_end_time']):0;
            $rs = Db::name("product_quan")->insert($list);
            // var_dump($list);
            // return $this->error($rs);
            if($rs){
                return $this->success('添加成功');
            }
            else{
                return $this->error('添加失败，请重试');
            }
        }
        else{
            $template = $this->request->param('template', 'form');
            return $this->fetch($template);
        }
    }

    public function edit(){
        if ($this->request->isPost()) {// 数据验证
            $list = $this->request->post();
            // var_dump($list);exit;
            $list['ctime'] = time();
            $list['get_bg_time'] = $list['get_bg_time']?strtotime($list['get_bg_time']):0;
            $list['get_end_time'] = $list['get_end_time']?strtotime($list['get_end_time']):0;
            $list['use_bg_time'] = $list['use_bg_time']?strtotime($list['use_bg_time']):0;
            $list['use_end_time'] = $list['use_end_time']?strtotime($list['use_end_time']):0;
            if(!$list['id']){
                return $this->error('参数错误');
            }
            else{
                $id = $list['id'];
                unset($list['id']);
                $rs = Db::name("product_quan")->where('id',$id)->update($list);
                // var_dump($list);
                // return $this->error($rs);
                if($rs){
                    return $this->success('修改成功');
                }
                else{
                    return $this->error('修改失败，请重试');
                }
            }
        }
        $id = input("id/d");
        $formData = Db::name("product_quan")->where("id",$id)->find();
        $formData['get_bg_time'] = $formData['get_bg_time']>0?date('Y-m-d H:i:s',$formData['get_bg_time']):'';
        $formData['get_end_time'] = $formData['get_end_time']>0?date('Y-m-d H:i:s',$formData['get_end_time']):'';
        $formData['use_bg_time'] = $formData['use_bg_time']>0?date('Y-m-d H:i:s',$formData['use_bg_time']):'';
        $formData['use_end_time'] = $formData['use_end_time']>0?date('Y-m-d H:i:s',$formData['use_end_time']):'';
        $this->assign('formData', $formData);
        $template = $this->request->param('template', 'form');
        return $this->fetch($template);
    }

    function del(){
        $id = input("get.id/d");
        $rs = Db::name('product_quan')->where('id',$id)->delete();
        if($rs){
            return $this->success('删除成功');
        }
        else{
            return $this->error('删除失败，请重试');
        }
        
    }


    
}

?>